/**
 * Created by kevin on 2017/6/7.
 */
var model= require('./model');
require('../utils/date');

var Components = model.createModel({
    name : String,
    version : String,
    updating : {type:Boolean,default:false},
    url : String,
    type : Number, // 1 ios 2 android
    lastUpdateTime : {
        type:Date,
        default:Date.now
    },
    gitUser : {type :model.objId,ref:'GitUsers'},
    lastUpdateUser : {type :model.objId,ref:'Users'},
    lastUpdateSuccess : {type : Boolean,default : true},
    updateCount : {type : Number,default : 0},
    updateSpend : {type : Number,default : 0},
    // repoName : {type: String,default : 'hui10-hui10-ios-hui10repos'},
    repo : {type:model.objId,ref:'Repos'},
    robots : [{type:model.objId,ref:'Robots'}],
    ctime : {type : Date,default:Date.now}
},'Components');

function listCallBack(err,list,callback) {
    if (err) {
        if (callback) {
            callback(err,list);
        }
    } else  {
        var cList = list.map(function (com) {
            if (com && com.lastUpdateTime) {
                com.lastUpdateTime = com.lastUpdateTime.dynamicFotmat();
            }
            // com.lastUpdateTime = com.lastUpdateTime.Format('yyyy-MM-dd hh:mm:ss');
            return com;
        });
        if (callback) {
            callback(err,cList);
        }
    }
}

function singleCallBack(err,doc,callback) {
    if (err) {
        if (callback) {
            callback(err,doc);
        }
    } else  {
        if (doc && doc.lastUpdateTime) {
            doc.lastUpdateTime = doc.lastUpdateTime.dynamicFotmat();
        }
        if (callback) {
            callback(err,doc);
        }
    }
}

Components.getListByType = function(type,callback) {

    Components.find({type:type},null,{ lean: true ,sort:{ctime:-1}}).populate('robots').populate('gitUser').populate('repo').populate('lastUpdateUser',{_id:0,passwd:0}).exec(function (err,list) {
        listCallBack(err,list,callback);
    });
};

Components.getAndroidList = function(callback) {
    Components.find({type:2},null,{ lean: true ,sort:{ctime:-1}}).populate('robots').populate('gitUser').populate('repo').populate('lastUpdateUser',{_id:0,passwd:0}).exec(function (err,list) {
        listCallBack(err,list,callback);
    });
};

Components.getIOSList = function(callback) {
    Components.find({type:1},null,{ lean: true ,sort:{ctime:-1}}).populate('robots').populate('gitUser').populate('repo').populate('lastUpdateUser',{_id:0,passwd:0}).exec(function (err,list) {
        listCallBack(err,list,callback);
    });
};

Components.getAll = function(callback) {
    Components.find({},null,{ lean: true ,sort:{ctime:-1}}).populate('robots').populate('gitUser').populate('repo').populate('lastUpdateUser',{_id:0,passwd:0}).exec(function (err,list) {
        listCallBack(err,list,callback);
    });
};

Components.getById = function (id,callback) {
    Components.findById(id,null,{ lean: true ,sort:{ctime:-1}}).populate('robots').populate('gitUser').populate('repo').populate('lastUpdateUser',{_id:0,passwd:0}).exec(function (err,com) {
        singleCallBack(err,com,callback);
    });
};

Components.createUniqueName = function (doc,callback) {
    Components.findOne({name : doc.name},function (err,delegater) {
        if (delegater) {
            if (callback) {
                callback(new Error('模块已存在',null));
            }
        } else  {
            Components.create(doc,callback);
        }
    });
};

Components.edit = function (id,doc,callback) {
    Components.findByIdAndUpdate(id, doc, callback);
};

Components.changeStatus = function () {
    Components.update({},{updating:false});
}

module.exports= Components;
